// pages/classsort/classsort.js
const app = getApp()

Page({

    data: {
        classlist: [],
    },
    getwxmlcode(str, cal) {
        const query1 = wx.createSelectorQuery()
        query1.select(str).boundingClientRect()
        query1.selectViewport().scrollOffset()
        query1.exec((res) => {
            if (cal) cal(res[0])
        })
    },
    listitemmove(e) {
        // console.log(e)
        if (e.type == "touchmove") {
            var movetop = e.touches[0].pageY - this.data.itemheight;
            var moveoutindex = parseInt((movetop - this.data.jianqu) / this.data.jiange);
            if (e.currentTarget.dataset.index <= moveoutindex) moveoutindex++;
            this.moveoutindex = moveoutindex;
            this.setData({
                nowmoveindex: e.currentTarget.dataset.index,
                movetop,
                moveoutindex
            })
        } else {
            let index = e.currentTarget.dataset.index,
                score = this.data.classlist;
            let data = {
                ...score[index]
            };
            score.splice(index, 1);
            if (index <= this.moveoutindex - 1) this.moveoutindex--;
            score.splice(this.moveoutindex, 0, data);
            this.setData({
                classlist: score,
                moveoutindex: -1,
                nowmoveindex: -1
            });
        }
    },
    onShow: function () {},

    lastsubmit() {
        console.log(this.data.classlist)
        var classlist = []
        for (var i = 0; i < this.data.classlist.length; i++) {
            classlist.push(this.data.classlist[i].classID)
        }
        this.sendData(classlist)
    },

    onLoad: function (options) {

        this.getData()

    },
    onShow: function () {
        // 页面显示
        console.log("onShow");

    },
    onReady: function () {
        // 页面渲染完成
        console.log("onReady");
    },
    onHide: function () {
        console.log(22);


        // 页面隐藏
        console.log("onHide");
    },
    onUnload: function () {
        // 页面关闭
        console.log("onUnload");
    },
    getData: function () {
        var that = this;
        var data = {
            "userid": app.globalData.userId,
            "schoolid": app.globalData.schoolId,
            "appType": app.globalData.appType,
            "appKey": app.globalData.appKey,
            "timeSpan": app.globalData.timespan,
            "mobileSerialnumber": ""
        }
        wx.request({
            url: app.globalData.serverUrl,
            data: {
                "data": JSON.stringify(data),
                'act': "get_classorderlist"
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            },
            success: function (res) {
                console.log(res);
                if (res.data.status == 0) {
                    var list = res.data.data
                    that.setData({
                        classlist: list,
                    })
                    that.getwxmlcode("#movebox", (resp) => {
                        that.setData({
                            movebox: resp
                        })
                        setTimeout(() => {
                            that.getwxmlcode("#movelist0", (res) => {
                                that.setData({
                                    movelist0: res
                                })
                                var jiange = res.top - resp.top;
                                var yiban = res.bottom - res.top + jiange;
                                that.setData({
                                    itemheight: res.bottom - res.top,
                                    jiange: yiban, //两条中间到另一条的距离
                                    jianqu: resp.top - (res.bottom - res.top) / 2, //位置要减去距离
                                })
                            })
                        }, 300)
                    })
                } else {
                    wx.showToast({
                        title: "获取数据失败",
                        icon: 'none',
                        duration: 1000,
                        mask: true
                    })
                }
            },
            fail: function (res) {
                wx.showToast({
                    title: "获取数据失败",
                    icon: 'none',
                    duration: 1000,
                    mask: true
                })
                console.log('submit fail');
            },
            complete: function (res) {
                console.log('submit complete');
            }
        })
    },


    sendData: function (classlist) {
        var that = this;
        var data = {
            "userid": app.globalData.userId,
            "schoolid": app.globalData.schoolId,
            "appType": app.globalData.appType,
            "appKey": app.globalData.appKey,
            "timeSpan": app.globalData.timespan,
            "mobileType": app.globalData.mobileType,
            "classList": classlist,
            "mobileSerialnumber": ""
        }
        wx.request({
            url: app.globalData.serverUrl,
            data: {
                "data": JSON.stringify(data),
                'act': "send_classorder"
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            },
            success: function (res) {
                console.log(res);
                if (res.data.status == 0) {
                    wx.showToast({
                        title: "班级排序成功",
                        icon: 'none',
                        duration: 1000,
                        mask: true
                    })
                } else {
                    wx.showToast({
                        title: "班级排序失败",
                        icon: 'none',
                        duration: 1000,
                        mask: true
                    })
                }
            },
            fail: function (res) {
                wx.showToast({
                    title: "班级排序失败",
                    icon: 'none',
                    duration: 1000,
                    mask: true
                })
                console.log('submit fail');
            },
            complete: function (res) {
                console.log('submit complete');
            }
        })
    },



})